package cd.cdyb.sms.utils;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.SimpleTimeZone;
import java.util.TimeZone;

/**
 * @author Junpeng.Su
 * @create 2017-07-16 下午 7:03
 * @description
 */
public class DateHelperPlus {

    /**
     * TODO 后期待改善
     * 获取当前时间
     *
     * @return 当前时间
     */
    public static Calendar getNow() {
        return Calendar.getInstance();
    }

    /**
     * 获取当前时间
     *
     * @return 当前时间
     */
    public static Date getCurrentTime() {

        String formatedDateString = getFormatedDateString(8);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date parse = sdf.parse(formatedDateString);
            return parse;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取当前时间
     *
     * @return 当前时间
     */
    public static LocalDateTime getCurrentLocalDateTime() {
        return LocalDateTime.now();
    }

    /**
     * 获取指定日期格式日期
     *
     * @param pattern 日期格式
     * @return 格式日期
     */
    public static String getFormatTime(String pattern) {
        Date time = Calendar.getInstance().getTime();
        return DateFormatUtils.format(time, pattern);
    }

    public static Date getParseTime(String time) {
        SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date parse = dateTimeFormat.parse(time);
            return parse;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void getCurrentFormatTime(String flag) {

        Date time = Calendar.getInstance().getTime();
        String format = DateFormatUtils.format(time, "yyyy-MM-dd HH:mm:ss");
        System.out.println(flag + ":" + format);
    }

    public static Date getFormatDateToNoon(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 12);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static Date getTomorrow(Date date) {
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        String year = String.valueOf(now.get(Calendar.YEAR));
        String month = String.valueOf(now.get(Calendar.MONTH) + 1);
        String day = String.valueOf(now.get(Calendar.DAY_OF_MONTH) + 1);
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        try {
            Date today = sdf.parse(month + "/" + day + "/" + year + " " + "00" + ":00:00");
            String format = sdf.format(today);
            System.out.println(format);
            return today;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }


    public static String getFormatedDateString(float timeZoneOffset){
        if (timeZoneOffset > 13 || timeZoneOffset < -12) {
            timeZoneOffset = 0;
        }

        int newTime=(int)(timeZoneOffset * 60 * 60 * 1000);
        TimeZone timeZone;
        String[] ids = TimeZone.getAvailableIDs(newTime);
        if (ids.length == 0) {
            timeZone = TimeZone.getDefault();
        } else {
            timeZone = new SimpleTimeZone(newTime, ids[0]);
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setTimeZone(timeZone);
        return sdf.format(new Date());
    }
}
